package com.example.node.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import com.example.node.MainActivity;
import com.example.node.R;
import com.example.node.adapter.ViewPagerAdapter;
import com.example.node.utils.ScreenUtils;
import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements ViewPager.OnPageChangeListener, View.OnTouchListener {

    private ViewPager mViewPager;
    private View viewOne,viewTwo,viewThree;
    private List<View> mList=new ArrayList<>();
    //当前的ITEM
    private int currentItem;
    private int startX,endX=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initData();
        initView();
    }

    private void initData(){
        //加载三个View
        viewOne=View.inflate(this,R.layout.layout_guide_view_one_item,null);
        viewTwo=View.inflate(this,R.layout.layout_guide_view_two_item,null);
        viewThree=View.inflate(this,R.layout.layout_guide_view_three_item,null);
        mList.add(viewOne);
        mList.add(viewTwo);
        mList.add(viewThree);
    }

    private void initView() {
        mViewPager=$(R.id.mViewPager);
        //设置适配器
        mViewPager.setAdapter(new ViewPagerAdapter(mList));
        //监听滑动事件
        mViewPager.addOnPageChangeListener(this);
        //监听触摸事件
        /**
         * ViewPager滑动
         * 在ViewPager尾页 右滑监听 mainActivity
         */
        mViewPager.setOnTouchListener(this);
    }

    public <T> T $(int viewID){
        return (T) findViewById(viewID);
    }

    /**
     * 监听滑动事件
     */
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        //状态改变
        currentItem=i;
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    /*
    * 触摸事件
    * */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        /**
         * 1.尾页
         * 2.右滑
         * 按下
         * 抬起
         * 移动
         */
        switch (event.getAction()){
            //按下
            case MotionEvent.ACTION_DOWN:
                startX = (int) event.getX();
                break;

            //抬起
            case MotionEvent.ACTION_UP:
                endX= (int) event.getX();
                //判断是否是最后一页
                if(currentItem==(mList.size()-1)){
                    //说明你有滑动距离
                    if(startX-endX>0){
                        //觉得你是左滑
                        if(startX-endX>= ScreenUtils.getWidth(this)/4){
                            startActivity(new Intent(GuideActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                }
                break;
        }
        return false;
    }
}
